<?php
/*
 * Created on 30.12.2006
 *
 */
 
m2import('phpinchains/util/db/Value.php');
 
/**
 * Represents a where clause as an object structure
 */

class phpinchains_util_db_Where
{
    var $boolop;
    var $parts = array();
    
    /**
     * 
     * @param boolean or  If true the list of expressions is connected by OR
     *                      operator, else it is connected by AND operator
     */
    function phpinchains_util_db_Where($or = false)
    {
        $this->boolop = ($or ? 'OR' : 'AND');
    }
    
    /**
     * Add a parts of the where clause.
     * 
     * Op means operator
     * 
     * @param mixed add Array in form array('op' => $op, 'lhs' => $lhs, 'rhs' => $rhs)
     *                  or instance of phpinchains_util_db_Where.
     */
    function add ($add)
    {
        if (!is_object($add))
        {
            if (empty($add['op']))
                return trigger_error("Operator is always needed!");
            if ($add['op'] < 11 && (empty($add['lhs']) || empty($add['rhs'])))
                return trigger_error("Operator needs left and right hand side!");
            if ($add['op'] > 10 && (empty($add['lhs'])))
                return trigger_error("Operator needs left hand side!");
            if (($add['op'] == 9 || $add['op'] == 10) 
                    && (!is_array($add['rhs'])))
                return trigger_error("Operator needs right hand side to be an array!");
        }
        $this->parts[] = $add;
    }
    
    /**
     * Creates a value object
     */
    function value ($val)
    {
        return new phpinchains_util_db_Value($val);
    }
    
    /**
     * 
     */
    function quoteSQL($str)
    {
        if (!get_magic_quotes_gpc())
            $str = addslashes($str);
        return "'".$str."'";
    }
    
    /**
     * Dumps 'where clause' to SQL.
     * 
     * This can be used with all abstraction layers, which can make use of raw SQL.
     * For abstractions like propel an ezpdo the developer has to do things by himself.
     * 
     * @return string SQL string for where clause 
     */
    function dumpSQL()
    {
        $parts = array();
        foreach ($this->parts as $part)
        {
            if (is_object($part))
                $parts[] = '('.$part->dumpSQL().')';
            else
                if ($part['op'] < 9)
                {
                    $op = '';
                    switch ($part['op'])
                    {
                        case 1:
                            $op = '=';
                            break;
                        case 2:
                            $op = '<>';
                            break;
                        case 3:
                            $op = '>';
                            break;
                        case 4:
                            $op = '<';
                            break;
                        case 5:
                            $op = '>=';
                            break;
                        case 6:
                            $op = '<=';
                            break;
                        case 7:
                            $op = 'LIKE';
                            break;
                        case 8:
                            $op = 'NOT LIKE';
                            break;
                    }
                    $lhs = (is_object($part['lhs'] && strtolower(get_class($part['lhs'])) == 'phpinchains_util_db_value') ? $part['lhs']->dump() : $part['lhs']);
                    $lhs = (is_object($part['rhs'] && strtolower(get_class($part['rhs'])) == 'phpinchains_util_db_value') ? $part['rhs']->dump() : $part['rhs']);
                    $parts[] = $part['lhs'].' '.$op.' '.$part['rhs'];
                }
        }
    }
    
    // Operators: not as static properties for php4 compatibility
    /**
     * SQL operator =
     */
    static function EQUAL ()
    {
        return 1;
    } 
    /**
     * SQL operator <> or !=
     */
    static function NOT_EQUAL ()
    {
        return 2;
    } 
    /**
     * SQL operator >
     */
    static function GREATER_THAN ()
    {
        return 3;
    } 
    /**
     * SQL operator <
     */
    static function LESS_THAN ()
    {
        return 4;
    } 
    /**
     * SQL operator >=
     */
    static function GREATER_EQUAL ()
    {
        return 5;
    } 
    /**
     * SQL operator <=
     */
    static function LESS_EQUAL ()
    {
        return 6;
    } 
    /**
     * SQL operator LIKE
     */
    static function LIKE ()
    {
        return 7;
    } 
    /**
     * SQL operator NOT LIKE
     */
    static function NOT_LIKE ()
    {
        return 8;
    } 
    /**
     * SQL operator IN
     */
    static function IN ()
    {
        return 9;
    } 
    /**
     * SQL operator NOT IN
     */
    static function NOT_IN ()
    {
        return 10;
    } 
    /**
     * SQL operator IS NULL
     */
    static function IS_NULL ()
    {
        return 11;
    } 
    /**
     * SQL operator IS NOT NULL
     */
    static function IS_NOT_NULL ()
    {
        return 12;
    } 
}
?>
